//
//  ITMJRefreshHeadGifImagesTool.m
//  ITBE
//
//  Created by ITBE on 2019/3/7.
//  Copyright © 2019 ITBE. All rights reserved.
//

#import "ITMJRefreshHeadGifImagesTool.h"

@implementation ITMJRefreshHeadGifImagesTool
static NSMutableDictionary *_gifImages;
+(NSMutableDictionary *)gifImages
{
    if (_gifImages==nil) {
        _gifImages=[NSMutableDictionary dictionary];
    }
    return _gifImages;
}

+(void)gifImagesWithGif:(NSString *)gifName
{
    YYImage *gifImage = [YYImage imageNamed:gifName];
    if (gifImage) {
        NSMutableArray *images = [NSMutableArray array];
        for (NSInteger i=0; i < gifImage.animatedImageFrameCount;i++) {
            UIImage *image = [gifImage animatedImageFrameAtIndex:i];
            [images addObject:image];
        }
        [[self gifImages] setObject:images forKey:@"MJRefreshStateRefreshing"];
        
        NSMutableArray *idleImages = [NSMutableArray array];
        for (NSInteger j = 0; j < images.count / 2; j++) {
            UIImage *idleImage = [images objectAtIndex:j];
            [idleImages addObject:idleImage];
        }
        [[self gifImages] setObject:idleImages forKey:@"MJRefreshStateIdle"];
        UIImage *lastImage = [idleImages lastObject];
        [[self gifImages] setObject:@[lastImage] forKey:@"MJRefreshStatePulling"];
    }
}

+(NSArray *)imagesWithMJRefreshStateIdle
{
    return [[self gifImages] objectForKey:@"MJRefreshStateIdle"];
}

+(NSArray *)imagesWithMJRefreshStatePulling
{
    return [[self gifImages] objectForKey:@"MJRefreshStatePulling"];
}

+(NSArray *)imagesWithMJRefreshStateRefreshing
{
    return [[self gifImages] objectForKey:@"MJRefreshStateRefreshing"];
}

+(void)footerTitleWithMJRefreshStates:(MJRefreshAutoStateFooter *)footer
{
    [footer setTitle:[AppUtils titleForKey:@"Pull up to load more"] forState:MJRefreshStateIdle];
    [footer setTitle:[AppUtils titleForKey:@"Release to load more"] forState:MJRefreshStatePulling];
    [footer setTitle:[AppUtils titleForKey:@"Loading..."] forState:MJRefreshStateRefreshing];
    [footer setTitle:[AppUtils titleForKey:@"About to load more"] forState:MJRefreshStateWillRefresh];
    [footer setTitle:[AppUtils titleForKey:@"No more data"] forState:MJRefreshStateNoMoreData];
}

+(void)headerTitleWithMJRefreshStates:(MJRefreshStateHeader *)header
{
    [header setTitle:[AppUtils titleForKey:@"Drop down to refresh"] forState:MJRefreshStateIdle];
    [header setTitle:[AppUtils titleForKey:@"Release to refresh"] forState:MJRefreshStatePulling];
    [header setTitle:[AppUtils titleForKey:@"Refreshing..."] forState:MJRefreshStateRefreshing];
    [header setTitle:[AppUtils titleForKey:@"About to refresh"] forState:MJRefreshStateWillRefresh];
    [header setTitle:[AppUtils titleForKey:@"No more data"] forState:MJRefreshStateNoMoreData];
}
@end
